package com.shareinfo.adminManage.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.shareinfo.adminManage.sys.model.SysUserModel;
import com.shareinfo.adminManage.sys.query.SysUserQuery;
import com.shareinfo.adminManage.sys.service.SysUserService;
import com.shareinfo.base.pagination.PageInfo;

@Controller
@Scope("prototype")
@RequestMapping(value = "/adminManage/sys/sysUser")
public class SysUserController {

	@Resource(name = "sysUserService")
	private SysUserService sysUserService;

	@ModelAttribute
	public SysUserModel get(@RequestParam(required = false) java.lang.String id) {
		if (id != null) {
			return sysUserService.getById(id);
		} else {
			return new SysUserModel();
		}
	}

	@RequestMapping("login")
	public ModelAndView login(SysUserModel sysUserModel) {
		String username = "hp@163.com";
		String password = "123";
		Subject currentUser = SecurityUtils.getSubject();
		if (!currentUser.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			token.setRememberMe(false);
			String result;
			try {
				currentUser.login(token);
				result = "success";  
	        } catch (UnknownAccountException uae) {  
	            result = "failure";  
	        } catch (IncorrectCredentialsException ice) {  
	            result = "Password for account " + token.getPrincipal() + " was incorrect!";  
	        } catch (LockedAccountException lae) {  
	            result = "failure";  
	        } catch (AuthenticationException ae) {  
	            result = "failure";  
	        }  
			return new ModelAndView("redirect:/adminManage/sys/main.jsp");
		} else {// 重复登录
//			ShiroUser shiroUser = (ShiroUser) currentUser.getPrincipal();  
//            if(!shiroUser.getLoginName().equalsIgnoreCase(username)){//如果登录名不同  
//                currentUser.logout();  
//                result = login(currentUser,username,password);  
//            }
			return new ModelAndView("redirect:/adminManage");
		}
//		if (sysUserModel != null
//				&& "hp@163".equals(sysUserModel.getLoginName())) {
//			UsernamePasswordToken token = new UsernamePasswordToken(username,
//					password);
//			return new ModelAndView("redirect:/adminManage/sys/main");
//		}
	}

	/**
	 * 执行搜索
	 **/
	@RequestMapping(value = { "list", "" })
	@ResponseBody
	public Map<String, Object> list(SysUserQuery sysUserQuery,
			HttpServletRequest request, HttpServletResponse response,
			SysUserQuery query) {
		PageInfo<SysUserModel> pageInfo = sysUserService.selectList(sysUserQuery);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put(PageInfo.EASYUI_ROWS, pageInfo.getList());
		map.put(PageInfo.EASYUI_TOTAL, pageInfo.getTotalRecord());
		return map;
	}
	/*public ModelAndView list(SysUserModel sysUserModel,
			HttpServletRequest request, HttpServletResponse response,
			SysUserQuery query) {
		PageInfo<SysUserModel> pageInfo = sysUserService.selectList(
				new PageInfo<SysUserModel>(), sysUserModel);

		ModelAndView result = new ModelAndView("sysUserList");
		result.addObject("pageInfo", pageInfo);
		return result;
	}*/

	/**
	 * 查看对象
	 **/
	@RequestMapping(value = { "show" })
	public ModelAndView show(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		SysUserModel sysUser = (SysUserModel) sysUserService.getById(id);
		return new ModelAndView("sysUserShow", "sysUser", sysUser);
	}

	/**
	 * 进入新增页面
	 **/
	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response, SysUserModel sysUserModel)
			throws Exception {
		return new ModelAndView("sysUserCreate", "sysUser", sysUserModel);
	}

	/**
	 * 保存新增对象
	 **/
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response, SysUserModel sysUserModel)
			throws Exception {
		sysUserService.save(sysUserModel);
		return new ModelAndView("sysUserList");
	}

	/**
	 * 进入更新页面
	 **/
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		SysUserModel sysUserModel = (SysUserModel) sysUserService.getById(id);
		return new ModelAndView("sysUserEdit", "sysUser", sysUserModel);
	}

	/**
	 * 保存更新对象
	 **/
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));

		SysUserModel sysUserModel = (SysUserModel) sysUserService.getById(id);
		Map params = WebUtils.getParametersStartingWith(request, "");
		BeanUtilsBean beanUtils = new BeanUtilsBean();
		beanUtils.copyProperties(sysUserModel, params);
		sysUserService.update(sysUserModel);
		return new ModelAndView("sysUserList");
	}

	/**
	 * 删除对象
	 **/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		for (int i = 0; i < ids.length; i++) {
			sysUserService.deleteById(ids[i]);
		}
		return new ModelAndView("sysUserList");
	}

}
